package com.techwells.learningkites.controller;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;

import com.techwells.learningkites.R;
import com.techwells.learningkites.adapter.CourseViewPagerAdapter;
import com.techwells.learningkites.fragment.CourseFragment.MyOnPageChangeListener;
import com.techwells.taco.base.CommonBaseActivity;

public class TestActivity extends CommonBaseActivity {
	
	private List<ImageView> imageViews;
	private int[] imageResId;
	private ViewGroup tipGroup;
	private ImageView[] tips;
	private List<View> dots;
	private CourseViewPagerAdapter courseViewPagerAdapter;
	private ViewPager coursevViewPager;
	private int currentItem = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test);
		
coursevViewPager = (ViewPager) findViewById(R.id.vp_course);
		
		imageResId = new int[] { R.drawable.ad_1, R.drawable.ad_2, R.drawable.ad_1 };
		imageViews = new ArrayList<ImageView>();
		for (int i = 0; i < imageResId.length; i++) {  
            ImageView imageView = new ImageView(this);  
            imageView.setImageResource(imageResId[i]);  
            imageView.setScaleType(ScaleType.CENTER_CROP);  
            imageViews.add(imageView);  
        }  
		
		 tipGroup = (ViewGroup) findViewById(R.id.tip_group);
		 tips = new ImageView[imageViews.size()];
		 for (int i = 0; i < tips.length; i++) {
				ImageView imageView = new ImageView(this);
//				imageView.setLayoutParams(new LayoutParams(5, 5));
				tips[i] = imageView;
				if (i == 0) {
					tips[i].setBackgroundResource(R.drawable.dot_selected);
				} else {
					tips[i].setBackgroundResource(R.drawable.dot);
				}

				LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
						new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
								LayoutParams.WRAP_CONTENT));
				layoutParams.leftMargin = 5;
				layoutParams.rightMargin = 5;
				tipGroup.addView(imageView, layoutParams);
			}
		 courseViewPagerAdapter = new CourseViewPagerAdapter(this, new ArrayList<View>());
		 coursevViewPager.setAdapter(courseViewPagerAdapter);
		 coursevViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
//		 homePagelVp.setCurrentItem(currentItem);
	}
	
public class MyOnPageChangeListener implements OnPageChangeListener {
		
		
		@Override
		public void onPageScrollStateChanged(int arg0) {
			
		}

		// 当当前页面被滑动时调用
		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		// 当新的页面被选中时调用
		@Override
		public void onPageSelected(int item) {			
			currentItem = item;			
			if (item > imageViews.size()) {
				item = item % imageViews.size();
			}
			for (int i = 0; i < tips.length; i++) {
				if (i == item) {
					tips[i].setBackgroundResource(R.drawable.dot_selected);
				} else {
					tips[i].setBackgroundResource(R.drawable.dot);
				}
			}
		}

	}

}
